package com.yunnuo.system.role.controller;


import com.yunnuo.system.comm.controller.BaseController;
import com.yunnuo.system.comm.util.ResultData;
import com.yunnuo.system.comm.util.page.TableDataInfo;
import com.yunnuo.system.role.entity.Role;
import com.yunnuo.system.role.service.IRoleMenuService;
import com.yunnuo.system.role.service.IRoleService;
import com.yunnuo.system.users.entity.PageParams;
import net.sf.json.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @api {title} 角色信息comtroller
 * @apiName RoleController
 * @apiGroup System-RoleController
 * @apiDescription Created by 魏洪福(15064000912@163.com) on  2018/5/25.
 */
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    private String prefix = "system/role";

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleMenuService roleMenuService;

    @RequiresPermissions("system:role:view")
    @GetMapping()
    public String role()
    {
        return prefix + "/role";
    }

    @RequiresPermissions("system:role:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(PageParams pageParams)
    {
        return roleService.selectRoleListByPageParams(pageParams,startPage(pageParams));
    }

    /**
     * 新增角色
     */
    @RequiresPermissions("system:role:add")
    @GetMapping("/add")
    public String add(Model model)
    {
        return prefix + "/add";
    }

    /**
     * 修改角色
     */
    @RequiresPermissions("system:role:edit")
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") String roleId, Model model)
    {
        Role role = roleService.findVO(roleId);
        model.addAttribute("role", role);
        return prefix + "/edit";
    }

    /**
     * 保存角色
     */
    @RequiresPermissions("system:role:save")
    @PostMapping("/save")
    @ResponseBody
    public JSON save(Role role,String menuIds)
    {
        ResultData data = new ResultData();
        try{

            roleService.save(role,menuIds);
            data.setStat(ResultData.STAT_SUCCESS);
            data.setMessage("操作成功！");
        }catch (Exception e){
            e.printStackTrace();
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("操作失败！");
        }

        return data.toJson();
    }

    @RequiresPermissions("system:role:remove")
    @RequestMapping("/remove/{roleId}")
    @ResponseBody
    public JSON remove(@PathVariable("roleId") String roleId)
    {
        ResultData data = new ResultData();
        Role role = roleService.findVO(roleId);
        if (role == null)
        {
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("角色不存在！");
        }
        if (roleMenuService.selectCountUserRoleByRoleId(roleId) > 0)
        {
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("角色已分配不能删除！");
        }
        if (roleService.deleteRoleById(roleId) > 0)
        {
            data.setStat(ResultData.STAT_SUCCESS);
            data.setMessage("操作成功！");
        }
        return data.toJson();
    }

    @RequiresPermissions("system:role:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public JSON batchRemove(@RequestParam("ids[]") String[] ids)
    {
        ResultData data = new ResultData();
        try {
            roleService.delList(ids);
            data.setStat(ResultData.STAT_SUCCESS);
            data.setMessage("操作成功！");
        }catch (Exception e){
           e.printStackTrace();
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("操作失败！");
        }
        return data.toJson();

    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public String checkRoleNameUnique(Role role)
    {
        String uniqueFlag = "0";
        if (role != null)
        {
            uniqueFlag = roleService.checkRoleNameUnique(role);
        }
        return uniqueFlag;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree")
    public String selectMenuTree()
    {
        return prefix + "/tree";
    }

}